<!--
 * @Author: Bo Cai
 * @Mail: caibo0520@qq.com
 * @Date: 2021-12-22 12:32:02
 * @LastEditors: Bo Cai
 * @LastEditTime: 2021-12-22 23:55:02
-->
<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">{{ this.userForm.nickname }}收藏的课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li>
                  <a title="全部" @click="resetSearchObj()" href="">全部</a>
                </li>
                <li
                  v-for="(subjectOne, index) in subjectNestedList"
                  :key="index"
                  :class="{ current: oneIndex == index }"
                >
                  <a
                    :title="subjectOne.title"
                    href="#"
                    @click="searchOne(subjectOne.id, index)"
                    >{{ subjectOne.title }}</a
                  >
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li
                  v-for="(subjectTwo, index) in subSubjectList"
                  :key="index"
                  :class="{ current: twoIndex == index }"
                >
                  <a
                    :title="subjectTwo.title"
                    href="#"
                    @click="searchTwo(subjectTwo.id, index)"
                    >{{ subjectTwo.title }}</a
                  >
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
              <li
                :class="{
                  'current bg-orange': gmtCreateSort != '',
                }"
              >
                <a
                  title="最新发布"
                  href="javascript:void(0);"
                  @click="searchGmtCreate()"
                  >最新发布
                  <span :class="{ hide: gmtCreateSort == '' }">↓</span>
                </a>
              </li>
              <li
                :class="{
                  'current bg-orange': commentCountSort != '',
                }"
              >
                <a
                  title="最多评论"
                  href="javascript:void(0);"
                  @click="searchcommentCount()"
                  >最多评论
                  <span
                    :class="{
                      hide: commentCountSort == '',
                    }"
                    >↓</span
                  >
                </a>
              </li>
              <li
                :class="{
                  'current bg-orange': viewCountSort != '',
                }"
              >
                <a
                  title="最多浏览"
                  href="javascript:void(0);"
                  @click="searchViewCount()"
                  >最多浏览
                  <span :class="{ hide: viewCountSort == '' }">↓</span>
                </a>
              </li>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="data.total === 0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam"
              >你们还没有收藏一门课程哦...</span
            >
          </section>
          <!-- /无数据提示 结束-->
          <article class="comm-course-list">
            <ul class="of" id="bna">
              <!--开始遍历-->
              <li v-for="item in data.items" :key="item.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img
                      :src="item.cover"
                      class="img-responsive"
                      :alt="item.title"
                      style="height: 150px; width: 267px"
                    />
                    <div class="cc-mask">
                      <a
                        :href="'/course/' + item.id"
                        title="开始学习"
                        class="comm-btn c-btn-1"
                        >开始学习</a
                      >
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a
                      :href="'/course/' + item.id"
                      :title="item.title"
                      class="course-title fsize18 c-333"
                      >{{ item.title }}</a
                    >
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{ item.viewCount }}次浏览</i>
                      |
                      <i class="c-999 f-fA">{{ item.commentCount }}次评论</i>
                      |
                      <i class="c-999 f-fA"
                        >发布于：{{ item.gmtCreate.substr(0, 10) }}</i
                      >
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"></div>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <div>
          <div class="paging">
            <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
            <a
              :class="{ undisable: !data.hasPrevious }"
              href="#"
              title="首页"
              @click.prevent="gotoPage(1)"
              >首</a
            >
            <a
              :class="{ undisable: !data.hasPrevious }"
              href="#"
              title="前一页"
              @click.prevent="gotoPage(data.current - 1)"
              >&lt;</a
            >
            <a
              v-for="page in data.pages"
              :key="page"
              :class="{
                current: data.current == page,
                undisable: data.current == page,
              }"
              :title="'第' + page + '页'"
              href="#"
              @click.prevent="gotoPage(page)"
              >{{ page }}</a
            >
            <a
              :class="{ undisable: !data.hasNext }"
              href="#"
              title="后一页"
              @click.prevent="gotoPage(data.current + 1)"
              >&gt;</a
            >
            <a
              :class="{ undisable: !data.hasNext }"
              href="#"
              title="末页"
              @click.prevent="gotoPage(data.pages)"
              >末</a
            >
            <div class="clear" />
          </div>
        </div>
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
import loginApi from '@/api/login'
import cookie from 'js-cookie'
import courseApi from '@/api/course'
export default {
  data() {
    return {
      baseUrl: '',
      page: 1,
      data: {},
      subjectNestedList: [], // 一级分类列表
      subSubjectList: [], // 二级分类列表
      searchObj: {}, // 查询表单对象
      oneIndex: -1,
      twoIndex: -1,
      viewCountSort: '', // 按照浏览量排序
      gmtCreateSort: '', // 按照课程发布日期排序
      commentCountSort: '', // 按照课程评论量排序
      userForm: {},
    }
  },
  created() {
    // 获取用户信息
    this.findUserInfo()
    // 获取课程列表
    this.getPageList()
    // 获取课程分类列表
    this.getSubjectList()
    this.baseUrl = process.env.baseUrl
  },
  methods: {
    findUserInfo() {
      //调用接口 根据token获取用户信息，为了首页面显示
      loginApi.getLoginInfo(cookie.get('guli_token')).then((response) => {
        this.userForm = response.data.data.loginInfo
      })
    },
    // 分页切换的方法
    gotoPage(page) {
      courseApi
        .getCollectedPageList(page, 8, this.searchObj)
        .then((response) => {
          this.data = response.data.data
        })
    },
    // 查看课程列表的方法
    getPageList() {
      courseApi.getCollectedPageList(1, 8, this.searchObj).then((response) => {
        this.data = response.data.data
      })
    },
    // 获取一级分类列表
    getSubjectList() {
      courseApi.getSubjectOne().then((response) => {
        this.subjectNestedList = response.data.data.items
      })
    },
    // 根据一级分类查询二级分类
    searchOne(subjectParentId, index) {
      // 把index值传递给oneIndex为了生成样式
      this.oneIndex = index
      this.twoIndex = -1
      this.searchObj.subjectId = ''
      // 二级分类列表置为空
      this.subSubjectList = []
      // 把一级分类点击id值，赋值给searchObj
      this.searchObj.subjectParentId = subjectParentId
      // 点击
      this.gotoPage(1)
      // 拿着所有的一级分类id 和所有的一级分类id进行比较
      // 如果id相同，从一级分类里面获取对应的二级分类
      for (let i = 0; i < this.subjectNestedList.length; i++) {
        // 获取每一个一级分类
        var oneSubject = this.subjectNestedList[i]
        // 比较id是否相同
        if (subjectParentId == oneSubject.id) {
          // 从一级分裂里面获取对应的二级分类
          this.subSubjectList = oneSubject.children
        }
      }
    },
    //点击二级分类，查询数据
    searchTwo(subjectId, index) {
      this.twoIndex = index
      this.searchObj.subjectId = subjectId
      this.gotoPage(this.page)
    },
    resetSearchObj() {
      this.searchObj = {}
    },
    //浏览量排序查询
    searchViewCount() {
      this.viewCountSort = '1'
      this.gmtCreateSort = ''
      this.commentCountSort = ''
      this.searchObj.viewCountSort = this.viewCountSort
      this.searchObj.gmtCreateSort = this.gmtCreateSort
      this.searchObj.commentCountSort = this.commentCountSort
      this.gotoPage(this.page)
    },
    //课程发布时间查询
    searchGmtCreate() {
      this.viewCountSort = ''
      this.gmtCreateSort = '1'
      this.commentCountSort = ''
      this.searchObj.viewCountSort = this.viewCountSort
      this.searchObj.gmtCreateSort = this.gmtCreateSort
      this.searchObj.commentCountSort = this.commentCountSort
      this.gotoPage(this.page)
    },
    //评论量排序查询查询
    searchcommentCount() {
      this.viewCountSort = ''
      this.gmtCreateSort = ''
      this.commentCountSort = '1'
      this.searchObj.viewCountSort = this.viewCountSort
      this.searchObj.gmtCreateSort = this.gmtCreateSort
      this.searchObj.commentCountSort = this.commentCountSort
      this.gotoPage(this.page)
    },
  },
}
</script>
